package com.xohm.properties;

import java.io.FileInputStream;
import java.io.File;
import java.io.FileWriter;
import java.io.BufferedWriter;
import org.xml.sax.InputSource;
import com.xohm.base.logging.XohmLogger;

/**
 * Xohm Connection Manager
 *
 * Licensed Material - Property of Sprint Nextel
 * Restricted Materials of Sprint Nextel
 * (C) Copyright Sprint Nextel Corp. 2008 - All Rights Reserved.
 *
 * This class allow you to define a properties class with attributes that will
 * be set from a loaded xml configuration document in the application directory
 * path.  The format of the XML is as follows:<br><br>
 * 
 * <ConnectionManagerConfiguration><br>
 *	<Properties class="com.xohm.base.Properties"><br>
 *		<Attribute name="debug" type="boolean" value="true"/><br>
 *		<Attribute name="numberOfLogs" type="int" value="30"/><br>
 *	</Properties><br>
 *	<Properties class="com.xohm.session.Properties"><br>
 *		<Attribute name="logStatisticsEvery" type="int" value="30"/><br>
 *		<Attribute name="checkConnectedEvery" type="int" value="1000"/><br>
 *	</Properties><br>
 *	</ConnectionManagerConfiguration><br>
 *
 * @author Robin Katzer
 */
public class Configuration
{

	public Configuration()
	{
	}
	
	/**
	 * This method retrieves the properties as defined in the config file "config.xml"
	 * and uses reflection to set the attributes within each class.  If the class or attribute
	 * does not exists then it just goes onto the next attribute in the cofig doc.  If
	 * the config.xml doc is not found it will just return without setting any values.<br><br>
	 */
	public static void readProperties()
	{
		try
		{
			// Open an input stream for the parser
			File file = new File("config.xml");
			if (file.canRead())
			{
				FileInputStream fileInStream = new FileInputStream(file);
				if (fileInStream != null)
				{
					InputSource is = new InputSource(fileInStream);
					
					// Parse it using a SAX parser to set the properties attributes
					ConfigXMLParserHandler.parseConfigXML(is, true);
					
					fileInStream.close();
				}
			}
		}
		catch (Exception e)
		{
			XohmLogger.warningPrintln("Exception in Configuration().readProperties(): "+e.toString(), null, null);
		}
	}

	/**
	 * This method save the properties as defined in the config file "config.xml"
	 * and uses reflection to get the attributes within each class.  If the class or attribute
	 * does not exists then it just goes onto the next attribute in the cofig doc.  If
	 * the config.xml doc is not found it will just return without saving any values.<br><br>
	 */
	public static void saveProperties()
	{
		try
		{
			// Open an input stream for the parser
			File file = new File("config.xml");
			if (file.canRead())
			{
				FileInputStream fileInStream = new FileInputStream(file);
				if (fileInStream != null)
				{
					InputSource is = new InputSource(fileInStream);
					
					// Parse it using a SAX parser to set the properties attributes
					ConfigXMLParserHandler.parseConfigXML(is, false);
					fileInStream.close();
					
					// Save to configuration file
					String updatedXml = ConfigXMLParserHandler.updatedXml.toString();
					if (file.canWrite())
					{
						file.createNewFile();
						FileWriter fstream = new FileWriter(file.getName());
						BufferedWriter out = new BufferedWriter(fstream);
						out.write(updatedXml);
						out.close();
					}
				}
			}
		}
		catch (Exception e)
		{
			XohmLogger.warningPrintln("Exception in Configuration().saveProperties(): "+e.toString(), null, null);
		}
	}
}
